<?php


namespace app\api\controller\v1;


use app\api\controller\BaseController;
use app\api\model\GrActivity as GrActivityModel;
use app\api\model\GrActivityOrder;
use app\api\service\activity_order\CreateGrOrder;
use app\api\validate\CheckGrActivity;
use app\lib\exception\ParameterException;
use app\lib\exception\TokenException;
use think\Db;
use think\Exception;
use app\api\service\Token as TokenService;

class GrActivity extends BaseController
{
    protected $beforeActionList = [
        'checkExclusiveScope' => ['only' => 'getDataList']
    ];

    /**
     * 获取个人活动列表
     */
    public function getDataList($page,$limit)
    {
        $time = time();
        $query = GrActivityModel::where('end_time','gt',$time)->with('imgs.img')->order('order_sort desc');
        $count = $query->count();
        $result = $query->page($page, $limit)->select()->toArray();
        $data['count'] = $count;
        $data['data'] = $result;
        return $data;
    }


    /**
     * 获取个人活动详情
     */
    public function getDataInfo($id)
    {
        $info = GrActivityModel::where('id',$id)->with('imgs.img')->find();
        $order = GrActivityOrder::where('activity_id',$info['id'])->find();
        if($order) {
            $info['order_id'] = $order['id'];
        } else {
            $info['order_id'] = '';
        }
        return $info;
    }

    /**
     * 创建个人活动详情
     */
    public function createData($fileList,$title,$start_time,$end_time,$num_status,$number,$man_num,$women_num,$content,$zy_content)
    {
        $openid = TokenService::getCurrentTokenVar("openid");
        $info = GrActivityModel::where('openid',$openid)->where('status',1)->find();
        if($info) {
            throw new ParameterException([
                'msg' => [
                    'gr' => '已有一个正在申请的活动!'
                ]
            ]);
        }
        (new CheckGrActivity())->goCheck();
        if($num_status == 1) {
            if(!$number) {
                throw new ParameterException([
                    'msg' => [
                        'gr' => '请输入参加活动人数'
                    ]
                ]);
            }
        }
        if($num_status == 2) {
            if(!$man_num || !$women_num) {
                throw new ParameterException([
                    'msg' => [
                        'gr' => '请输入参加活动人数'
                    ]
                ]);
            }
        }
        Db::startTrans();
        try{
            $mode = GrActivityModel::create([
                'title' => $title,
                'openid' => $openid,
                'num_status' => $num_status,
                'number' => $number,
                'men_num' => $man_num,
                'women_num' => $women_num,
                'start_time' => strtotime($start_time),
                'end_time' => strtotime($end_time),
                'content' => $content,
                'zy_content' => $zy_content,
            ]);
            $img_data = [];
            if($fileList) {
                foreach ($fileList as $k=>$v){
                    $img_data[$k] = ['img_id'=>$v['img_id']];
                }
                $mode->imgs()->saveAll($img_data);
            }
            Db::commit();
            return writeJson(201, [], '添加成功,已提交后台审核！');
        }catch (Exception $ex){
            Db::rollback();
            throw new ParameterException([
                'msg' => [
                    'gr' => '添加失败!'
                ]
            ]);
        }
    }

    /**
     * 创建个人订单
     */
    public function createOrder($id)
    {
        $order = new CreateGrOrder($id);
        return $order->entrance();
    }

    /**
     * 删除个人订单
     */
    public function deleteOrder($id)
    {
        $user = TokenService::judgeUser();
        $info = GrActivityOrder::where('id',$id)
            ->where('openid',$user['openid'])
            ->find();
        if(!$info) {
            throw new ParameterException([
                'msg' => [
                    'order' => '订单不存在'
                ]
            ]);
        }
        GrActivityOrder::destroy($info['id']);
        return writeJson(200,[
            'msg' => '完成'
        ]);
    }

    /**
     * 获取个人活动创建信息
     */
    public function getActivityUseList()
    {
        $userInfo = TokenService::judgeUser();
        return GrActivityModel::where('openid',$userInfo['openid'])
            ->with('imgs.img')
            ->order('order_sort desc')
            ->select()->toArray();
    }
}